package cn.iocoder.yudao.module.system.dal.mysql.equivalentscorerank;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.equivalentscorerank.EquivalentScoreRankDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 等效位次和等效分 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface EquivalentScoreRankMapper extends BaseMapperX<EquivalentScoreRankDO> {

    /**
     * 根据考试ID和学生ID查询等效位次和等效分
     *
     * @param examId 考试ID
     * @param studentId 学生ID
     * @return 等效位次和等效分记录
     */
    default EquivalentScoreRankDO selectByExamIdAndStudentId(Long examId, Long studentId) {
        return selectOne(new LambdaQueryWrapperX<EquivalentScoreRankDO>()
                .eq(EquivalentScoreRankDO::getExamId, examId)
                .eq(EquivalentScoreRankDO::getStudentId, studentId)
                .eq(EquivalentScoreRankDO::getCalculationStatus, 1) // 只查询计算成功的记录
                .orderByDesc(EquivalentScoreRankDO::getCalculationDate)
                .last("LIMIT 1"));
    }

    /**
     * 根据考试ID、学生ID和特控线ID查询等效位次和等效分
     *
     * @param examId 考试ID
     * @param studentId 学生ID
     * @param controlLineId 特控线ID
     * @return 等效位次和等效分记录
     */
    default EquivalentScoreRankDO selectByExamIdAndStudentIdAndControlLineId(Long examId, Long studentId, Long controlLineId) {
        return selectOne(new LambdaQueryWrapperX<EquivalentScoreRankDO>()
                .eq(EquivalentScoreRankDO::getExamId, examId)
                .eq(EquivalentScoreRankDO::getStudentId, studentId)
                .eq(EquivalentScoreRankDO::getControlLineId, controlLineId)
                .orderByDesc(EquivalentScoreRankDO::getCalculationDate)
                .last("LIMIT 1"));
    }

    /**
     * 根据学生ID查询所有等效位次和等效分记录
     *
     * @param studentId 学生ID
     * @return 等效位次和等效分记录列表
     */
    default List<EquivalentScoreRankDO> selectByStudentId(Long studentId) {
        return selectList(new LambdaQueryWrapperX<EquivalentScoreRankDO>()
                .eq(EquivalentScoreRankDO::getStudentId, studentId)
                .eq(EquivalentScoreRankDO::getCalculationStatus, 1)
                .orderByDesc(EquivalentScoreRankDO::getCalculationDate));
    }

    /**
     * 根据考试ID查询所有等效位次和等效分记录
     *
     * @param examId 考试ID
     * @return 等效位次和等效分记录列表
     */
    default List<EquivalentScoreRankDO> selectByExamId(Long examId) {
        return selectList(new LambdaQueryWrapperX<EquivalentScoreRankDO>()
                .eq(EquivalentScoreRankDO::getExamId, examId)
                .eq(EquivalentScoreRankDO::getCalculationStatus, 1)
                .orderByDesc(EquivalentScoreRankDO::getCalculationDate));
    }

    /**
     * 根据省份和考试类型查询等效位次和等效分记录
     *
     * @param province 省份
     * @param examType 考试类型
     * @return 等效位次和等效分记录列表
     */
    default List<EquivalentScoreRankDO> selectByProvinceAndExamType(String province, String examType) {
        return selectList(new LambdaQueryWrapperX<EquivalentScoreRankDO>()
                .eq(EquivalentScoreRankDO::getProvince, province)
                .eq(EquivalentScoreRankDO::getExamType, examType)
                .eq(EquivalentScoreRankDO::getCalculationStatus, 1)
                .orderByDesc(EquivalentScoreRankDO::getCalculationDate));
    }

    /**
     * 检查是否存在指定的等效位次和等效分记录
     *
     * @param examId 考试ID
     * @param studentId 学生ID
     * @param controlLineId 特控线ID
     * @return 是否存在
     */
    default boolean existsByExamIdAndStudentIdAndControlLineId(Long examId, Long studentId, Long controlLineId) {
        return selectCount(new LambdaQueryWrapperX<EquivalentScoreRankDO>()
                .eq(EquivalentScoreRankDO::getExamId, examId)
                .eq(EquivalentScoreRankDO::getStudentId, studentId)
                .eq(EquivalentScoreRankDO::getControlLineId, controlLineId)) > 0;
    }

    /**
     * 根据计算状态查询等效位次和等效分记录
     *
     * @param calculationStatus 计算状态
     * @return 等效位次和等效分记录列表
     */
    default List<EquivalentScoreRankDO> selectByCalculationStatus(Integer calculationStatus) {
        return selectList(new LambdaQueryWrapperX<EquivalentScoreRankDO>()
                .eq(EquivalentScoreRankDO::getCalculationStatus, calculationStatus)
                .orderByDesc(EquivalentScoreRankDO::getCalculationDate));
    }

    /**
     * 批量删除指定考试的等效位次和等效分记录
     *
     * @param examId 考试ID
     * @return 删除的记录数
     */
    default int deleteByExamId(Long examId) {
        return delete(new LambdaQueryWrapperX<EquivalentScoreRankDO>()
                .eq(EquivalentScoreRankDO::getExamId, examId));
    }

    /**
     * 批量删除指定学生的等效位次和等效分记录
     *
     * @param studentId 学生ID
     * @return 删除的记录数
     */
    default int deleteByStudentId(Long studentId) {
        return delete(new LambdaQueryWrapperX<EquivalentScoreRankDO>()
                .eq(EquivalentScoreRankDO::getStudentId, studentId));
    }

}